Visualizing Equities with knitr Parameters

Author

Robert W. Walker

The tidyquant package provides tools and data for visualizing and analysing equities. Here is an example using data from F. Hmm….

What happened? This is fairly cool.

Code
dat <- tq_get(toString(params$ticker))
dat %>% ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close)) +
    labs(title = paste(params$ticker,"Bar Chart"), y = "Closing Price", x = "") +
    theme_tq()

Or functions of the data, like returns.

Code
dat %>% tq_transmute(select= adjusted,
                 mutate_fun = periodReturn,
                 period     = "daily",
                 col_rename = "Ra") %>% as_tsibble(index=date) %>% autoplot()

Adding Some Interactives

The following are a few quick interactive plots.

Code
library(tidyquant)
library(tidyverse)
library(magrittr)
# Use tidyquant to get the data
# Slice off the most recent 120 days
dat.tail <- tail(dat, 120)
dat.tail %<>% mutate(
    open = round(open, digits=2),
    close = round(close, digits=2),
    high = round(high, digits=2),
    low = round(low, digits=2),
    adjusted = round(adjusted, digits=2)
    )

Let’s have a look at the data.

Code
library(DT)
datatable(dat.tail)

The Plot

There are a few charts specifically designed for OHLC data that are included in plotly. Here I want to deploy a basic one with one modification. I want daily increases in black and daily decreases in red.

Code
library(plotly)
# basic example of ohlc charts
# custom colors
i <- list(line = list(color = '#000000')) # black
d <- list(line = list(color = '#FF0000')) # red
# Create the figure
fig.2 <- dat.tail %>%
  plot_ly(x = ~date, type="ohlc",
          open = ~open, close = ~close,
          high = ~high, low = ~low,
          increasing = i, decreasing = d)
fig.2

References

Bache, Stefan Milton, and Hadley Wickham. 2022. Magrittr: A Forward-Pipe Operator for r. https://CRAN.R-project.org/package=magrittr.
Dancho, Matt, and Davis Vaughan. 2022. Tidyquant: Tidy Quantitative Financial Analysis. https://github.com/business-science/tidyquant.
Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Hyndman, Rob. 2023. Fpp3: Data for "Forecasting: Principles and Practice" (3rd Edition). https://CRAN.R-project.org/package=fpp3.
Müller, Kirill, and Hadley Wickham. 2022. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
O’Hara-Wild, Mitchell, Rob Hyndman, and Earo Wang. 2021. Fabletools: Core Tools for Packages in the Fable Framework. https://CRAN.R-project.org/package=fabletools.
———. 2022a. Fable: Forecasting Models for Tidy Time Series. https://CRAN.R-project.org/package=fable.
———. 2022b. Feasts: Feature Extraction and Statistics for Time Series. https://CRAN.R-project.org/package=feasts.
O’Hara-Wild, Mitchell, Rob Hyndman, Earo Wang, and Rakshitha Godahewa. 2022. Tsibbledata: Diverse Datasets for Tsibble. https://CRAN.R-project.org/package=tsibbledata.
Peterson, Brian G., and Peter Carl. 2020. PerformanceAnalytics: Econometric Tools for Performance and Risk Analysis. https://github.com/braverock/PerformanceAnalytics.
Ryan, Jeffrey A., and Joshua M. Ulrich. 2022a. Quantmod: Quantitative Financial Modelling Framework. https://CRAN.R-project.org/package=quantmod.
———. 2022b. Xts: eXtensible Time Series. https://github.com/joshuaulrich/xts.
Sievert, Carson. 2020. Interactive Web-Based Data Visualization with r, Plotly, and Shiny. Chapman; Hall/CRC. https://plotly-r.com.
Sievert, Carson, Chris Parmer, Toby Hocking, Scott Chamberlain, Karthik Ram, Marianne Corvellec, and Pedro Despouy. 2022. Plotly: Create Interactive Web Graphics via Plotly.js. https://CRAN.R-project.org/package=plotly.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2023. Lubridate: Make Dealing with Dates a Little Easier. https://CRAN.R-project.org/package=lubridate.
Ulrich, Joshua. 2021. TTR: Technical Trading Rules. https://github.com/joshuaulrich/TTR.
Wang, Earo, Dianne Cook, and Rob J Hyndman. 2020. “A New Tidy Data Structure to Support Exploration and Modeling of Temporal Data.” Journal of Computational and Graphical Statistics 29 (3): 466–78. https://doi.org/10.1080/10618600.2019.1695624.
Wang, Earo, Di Cook, Rob Hyndman, and Mitchell O’Hara-Wild. 2022. Tsibble: Tidy Temporal Data Frames and Tools. https://tsibble.tidyverts.org.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2022a. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2022b. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.
———. 2023. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2023. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Lionel Henry. 2023. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2023. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2023. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Xie, Yihui, Joe Cheng, and Xianying Tan. 2023. DT: A Wrapper of the JavaScript Library DataTables. https://github.com/rstudio/DT.
Zeileis, Achim, and Gabor Grothendieck. 2005. “Zoo: S3 Infrastructure for Regular and Irregular Time Series.” Journal of Statistical Software 14 (6): 1–27. https://doi.org/10.18637/jss.v014.i06.
Zeileis, Achim, Gabor Grothendieck, and Jeffrey A. Ryan. 2022. Zoo: S3 Infrastructure for Regular and Irregular Time Series (z’s Ordered Observations). https://zoo.R-Forge.R-project.org/.